<template>
  <div class="advancedForm">
    <el-form :inline="true" :label-position="'right'" :model="formData" class="form-inline">
      <el-row :class="{ 'not-show': byHeight && !isExpand }" :gutter="gutterWidth">
        <template v-for="(item, index) in columns">
          <el-col v-if="item.type" :span="6" v-show="byHeight ? true : index < showRow * 4 || isExpand">
            <el-form-item :label="item?.label" v-if="item.type === 'input'" style="width: 100%">
              <el-input v-model.trim="formData[item.name]" clearable
                :placeholder="item.placeholder ? item.placeholder : `请输入${item.label ? item.label : ''}`"
                maxlength="200" />
            </el-form-item>
            <el-form-item :label="item?.label" v-if="item.type === 'input2'" style="width: 100%">
              <div class="flex-justify-between ">
                <el-input v-model.trim="formData[item.Star]" clearable
                  :placeholder="item.placeholder1 ? item.placeholder1 : `请输入`" style="width:40%;" maxlength="200" />
                ~
                <el-input v-model.trim="formData[item.End]" clearable
                  :placeholder="item.placeholder2 ? item.placeholder2 : `请输入`" style="width:40%;" maxlength="200" />
              </div>
            </el-form-item>
            <el-form-item :label="item?.label" v-if="item.type === 'input3'" style="width: 100%">
              <el-input v-model.trim="item.value2" clearable :placeholder="item.placeholder ? item.placeholder : `请输入`"
                style="width:100%;" @change="(val) => inputChange(val, item, index)" maxlength="200">
                <template #prepend>
                  <el-select v-model="item.value" style="width: 115px" @change="(val) => selectChange(val, item, index)">
                    <el-option v-for="item2 in item.options" :key="item.valueO" :label="item2[item.labelO]"
                      :value="item2[item.valueO]" />
                  </el-select>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item :label="item?.label" v-if="item.type === 'select'" style="width: 100%">
              <el-select v-bind="item.attrs" v-model="formData[item.name]" clearable :filterable="true"
                :placeholder="item.placeholder ? item.placeholder : `请选择${item.label ? item.label : ''}`"
                style="width: 100%">
                <el-option v-for="item2 in item.options" :key="item.valueO" :label="item2[item.labelO]"
                  :value="item2[item.valueO]" />
              </el-select>
            </el-form-item>
            <el-form-item :label="item?.label" v-if="item.type === 'cascader'" style="width: 100%">
              <el-cascader v-bind="item.attrs" :options="item.options" clearable v-model="formData[item.name]"
                :filterable="item.filterable ? item.filterable : false"
                :placeholder="item.placeholder ? item.placeholder : `请选择${item.label}`" style="width: 100%" />
            </el-form-item>
            <el-form-item :label="item?.label" v-if="item.type === 'date-picker'" style="width: 100%">
              <el-date-picker v-bind="item.attrs" clearable v-model="formData[item.name]"
                :format="item.format ? item.format : 'YYYY-MM-DD'" type="date"
                :value-format="item.valueFormat ? item.valueFormat : 'YYYY-MM-DD'"
                :placeholder="item.placeholder ? item.placeholder : `请选择${item.label ? item.label : ''}`"
                style="width: 100%" />
            </el-form-item>
            <el-form-item :label="item?.label" v-if="item.type === 'daterange'" style="width: 100%">
              <el-date-picker v-bind="item.attrs" clearable v-model="formData[item.name]"
                :format="item.format ? item.format : 'YYYY-MM-DD'" type="daterange"
                :value-format="item.valueFormat ? item.valueFormat : 'YYYY-MM-DD'" start-placeholder="开始日期"
                end-placeholder="结束日期" style="width: 100%" @change="(val) => datepickerChange(val, item)" />
            </el-form-item>
            <el-form-item :label="item?.label" v-if="item.type === 'datetimerange'" style="width: 100%">
              <el-date-picker v-model="formData[item.name]" clearable type="datetimerange" date-format="YYYY-MM-DD"
                time-format="HH:mm:ss" :format="item.format ? item.format : 'YYYY-MM-DD HH:mm:ss'"
                :value-format="item.valueFormat ? item.valueFormat : 'YYYY-MM-DD HH:mm:ss'" start-placeholder="开始时间"
                end-placeholder="结束时间" style="width: 100%" @change="(val) => datepickerChange(val, item)" />
            </el-form-item>
            <el-form-item :label="item?.label" v-if="item.type === 'datetime'" style="width: 100%">
              <el-date-picker v-model="formData[item.name]" type="datetime" clearable date-format="YYYY-MM-DD"
                time-format="HH:mm:ss" :format="item.format ? item.format : 'YYYY-MM-DD HH:mm:ss'"
                :value-format="item.valueFormat ? item.valueFormat : 'YYYY-MM-DD HH:mm:ss'"
                :placeholder="item.placeholder ? item.placeholder : `请选择${item.label ? item.label : ''}`"
                style="width: 100%" />
            </el-form-item>
            <el-form-item :label="item?.label" v-if="item.type === 'datetime2'" style="width: 100%">
              <div class="flex-justify-between">
                <el-date-picker v-model="formData[item.Star]" type="datetime" clearable date-format="YYYY-MM-DD"
                  time-format="HH:mm:ss" :format="item.format ? item.format : 'YYYY-MM-DD HH:mm:ss'"
                  :value-format="item.valueFormat ? item.valueFormat : 'YYYY-MM-DD HH:mm:ss'"
                  :placeholder="item.placeholder1 ? item.placeholder1 : `请选择`" style="width: 45%" />
                <div>-</div>
                <el-date-picker v-model="formData[item.End]" type="datetime" clearable date-format="YYYY-MM-DD"
                  time-format="HH:mm:ss" :format="item.format ? item.format : 'YYYY-MM-DD HH:mm:ss'"
                  :value-format="item.valueFormat ? item.valueFormat : 'YYYY-MM-DD HH:mm:ss'"
                  :placeholder="item.placeholder2 ? item.placeholder2 : `请选择`" style="width: 45%" />
              </div>
            </el-form-item>
            <el-form-item :label="item?.label" v-if="item.type === 'year'" style="width: 100%">
              <el-date-picker v-model="formData[item.name]" type="year" date-format="YYYY" clearable
                :format="item.format ? item.format : 'YYYY'" :value-format="item.valueFormat ? item.valueFormat : 'YYYY'"
                style="width: 100%" :placeholder="item.placeholder ? item.placeholder : `请输入`" />
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="search-btn">
      <el-button type="primary" @click="onSubmit">查询</el-button>
      <el-button @click="resetForm()">重置</el-button>
      <el-button link type="primary" @click="isExpand = !isExpand" v-if="columns.length > 4 * showRow">
        {{ isExpand ? '合并' : '展开' }}
        <el-icon>
          <arrow-down v-if="!isExpand" />
          <arrow-up v-else />
        </el-icon>
      </el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {
  getCurrentInstance,
  onMounted,
  reactive,
  ref
} from 'vue';

const { proxy } = <any>getCurrentInstance();
let props = defineProps({
  // 宽度
  labelWidth: {
    default: '100px',
  },
  gutterWidth: {
    type: Number,
    default: 24,
  },
  showRow: {
    type: Number,
    default: 1,
  },
  columns: {
    type: Array,
    default: () => [],
  },
  byHeight: {
    type: Boolean,
    default: false,
  },
  formData: {
    type: Object,
    default: {}
  }
})
const emit = defineEmits(['submit', 'reset'])
// 收缩展开
const isExpand = ref(false)
const formParams = reactive(JSON.parse(JSON.stringify(props.formData)))

// 获取参数进行组装为 obj,进行搜索
const onSubmit = () => {
  emit('submit')
}
// 重置参数
const resetForm = () => {
  Object.keys(props.formData).forEach(val1 => {
    proxy.formData[val1] = formParams[val1] !== '' ? formParams[val1] : ''
  })
  props.columns.forEach((item, key) => {
    if (item?.type === 'input3') {
      proxy.columns[key].value = item.options[0][item.valueO]
      proxy.columns[key].value2 = ''
    }
  })
  emit('reset')
}
function datepickerChange(val, item) {
  if (val?.length) {
    proxy.formData[item.Star] = val[0]
    proxy.formData[item.End] = val[1]
  } else {
    proxy.formData[item.Star] = ''
    proxy.formData[item.End] = ''
  }
}
function inputChange(val, item, index) {
  item.options.forEach((val) => {
    proxy.formData[val[item.valueO]] = ''
  })
  proxy.formData[item.value] = val
}
function selectChange(val, item, index) {
  proxy.columns[index].value = val
  item.options.forEach((val) => {
    proxy.formData[val[item.valueO]] = ''
  })
  proxy.formData[val] = item.value2
}
onMounted(() => {
  let k = props.columns.findIndex(item => item.type === 'input3')
  if (k >= 0) {
    let item2 = props.columns[k]
    if (item2.options?.length) {
      let name = item2.options[0][item2.valueO]
      proxy.columns[k].value = name
      proxy.formData[name] = ''
    }
  }
})
defineExpose({
  onSubmit
})
</script>

<style lang="scss" scoped>
.advancedForm {
  display: flex;
  justify-content: space-between;

  .form-inline {
    flex: 1;
  }

  .el-form-item--default {
    margin-bottom: 14px;
  }

  .el-form--inline .el-form-item {
    width: 100%;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 40px;
  }

  .not-show {
    height: 40px;
    overflow: hidden;
  }
}
</style>
